#include "MiddleFilterAction.h"
#include "Work.h"



MiddleFilterAction::MiddleFilterAction () : AbstractAction(),
		_pFilter (new FilterMiddle (1))
{

}



void MiddleFilterAction::updatePreview (QRect viewport)
{
	if (_pFilter == NULL)
		return;

	Ptr< Layer > pCurLayer = __pTargetWork->getDoc ()->getCurrentLayer ();

	bool isPreview = true;
	if (viewport.isEmpty ()) {
		isPreview = false;
		viewport = pCurLayer->getRegion ();
	}

	QRect updateRegion = viewport & pCurLayer->getRegion ();

	QPoint oSrcImg = pCurLayer->getRegion ().topLeft ();
	QPoint oViewport = viewport.topLeft ();
	QPoint oUpdate = updateRegion.topLeft ();

	QRect processRegion = updateRegion;
	processRegion.translate (-oSrcImg);

	Ptr< QImage > pProcessSrc = new QImage (pCurLayer->getImage ()
		->copy (processRegion));
	Ptr< QImage > pProcessResult = _pFilter->process (pProcessSrc);

	if (isPreview)
		pCurLayer->setPreviewImage (pProcessResult);
	else
		pCurLayer->setImage (pProcessResult);
}



void MiddleFilterAction::rejectAction ()
{
	__pTargetWork->getDoc ()->getCurrentLayer ()->closePreview ();
}



void MiddleFilterAction::handleStartEvent ()
{
	__pTargetWork->getDoc ()->getCurrentLayer ()->openPreview ();
}



void MiddleFilterAction::applyAction ()
{
	updatePreview (QRect ());
	__pTargetWork->getDoc ()->getCurrentLayer ()->closePreview ();
}
